Pager code.

Paging state:
  $statelist->add('CORE', paging => {
    entry => sub {
      my ($self, $reason) = @_;
      $self->_page_send;
    },
    prompt => sub {
      my $siz = $_[0]->{prefs}{scr_width};
      "["
      . ($siz > 27 ? $_[0]->{page_name}.($_[0]->{page_pos}+1).'-'.($_[0]->{page_pend}+1).'/' . (scalar @{$_[0]->{page_lines}}) . " - " : '') 
      . "RET "
      . ($_[0]->{page_pend} >= $#{$_[0]->{page_lines}} ? 'to exit' : 'for more') 
      . ($siz > 57 ? ', back, redisplay, quit' : '')
      . "] ";
    },
    input => sub {
      my ($self, $input) = @_;
      my $isvt = ($self->{terminal_type} || '') =~ /^VT/;
      if ($input =~ /^\s*$/) {
        my $move_by = $self->_page_size - ($isvt ? 0 : 1);
        if ($self->{page_pend} >= $#{$self->{page_lines}}) {
          $self->setstate('command');
        } else {
          $self->{page_pos} += $move_by;
          if ($self->{page_pos} > ($#{$self->{page_lines}} - $self->_page_size) and !$isvt) {
            $self->{page_pos}++;
            # if we're sending less than a full page, don't repeat the first line, but if we're in VT mode, we don't repeat anyway
          }
          $self->send_str("\c[[1A\c[[K"); # erase prompt
          $self->_page_send;
        }
      } elsif ('back' =~ /^\Q$input/i) {
        $self->{page_pos} = 0 if ($self->{page_pos} -= ($self->_page_size - 1)) < 0;
        $self->_page_send; 
      } elsif ('redisplay' =~ /^\Q$input/i) {
        $self->_page_send;
      } elsif ('quit' =~ /^\Q$input/i) {
        $self->{page_lines} = [];
        $self->setstate('command');
      } else {
        $self->input_as('command', $input);
      }
    },
  });

Wrapping code:

# FIXME: this is old code

# sub format_wrap {
#   my ($self, $text, %opts) = @_;
# 
#   if (not defined $text) {
#     cluck "Undef passed to MConnection::format_wrap";
#     return;
#   }
# 
#   if (ref $text) {
#     croak "Cannot directly format_wrap SX";
#   }
#   
#   my @pict = $opts{picture} ? split /\n/, $opts{picture} : ();
# 
#   my $fmtext = '';
#   my $t;
#   my $wid = $self->scr_width;
#   foreach (split /\n/, $text) {
#     $t = $_;
#     {
#       $t = shift(@pict) . $t if @pict;
#       
#       my $char = length $t;
#       if ($self->display_length($t) > $wid) {
#         $char = $wid + 1;
#         $char-- while substr($t, $char - 1, 1) !~ /\s/;
#       }
#       my $out = substr($t, 0, $char);
#      
#       $out =~ s/\s+$//;
#       $fmtext .= "$out\n";
#       next if $char >= length $t;
#       $t = substr($t, $char);
#       redo if $t;
#     }
#   }
#   return $fmtext . join("\n", @pict);
# }

Pager utility functions:

# sub send_page {
#   my ($self, $buf, %opt) = @_;
#   my @lines = split /\n/, $self->format_wrap($buf);
#   #print Data::Dumper::Dumper(\@lines);
#   # $self->send("DEBUG: paging, ".(scalar @lines)." lines, psize is ".$self->_page_size);
#   if (@lines >= $self->{prefs}{scr_height} || $opt{force_prompt}) {
#     $self->{page_lines} = [@lines];
#     $self->{page_pos} = $opt{start} || 0;
#     $self->{page_name} = $opt{name} ? "$opt{name}, " : '';
#     $self->setstate('paging');
#   } else {
#     $self->send(join "\n", @lines);
#   }
# }
# 
# sub _page_size {
#   my ($self) = @_;
# 
#   return $self->{prefs}{scr_height} - 1;
# }
# 
# sub _page_send {
#   my ($self) = @_;
#   my $pp = $self->{page_pos};
#   my $pe = $self->{page_pend} = $pp + $self->_page_size - 1;
#   if ($pe > $#{$self->{page_lines}}) {
#     $pe = $#{$self->{page_lines}};
#     #$self->setstate('command');
#   }
#   $self->{page_pend} = $pe;
#   $self->send(join "\n", @{$self->{page_lines}}[$pp..$pe]);
# }


